package aero.champ.demo;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Tree;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@Theme("runo")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {
	private Label todoItemLabel = new Label("Todo to add: ");
	private TextField todoItemField = new TextField("buy a SSD");
	private TextField firstNameField = new TextField();
	private TextField lastNameField = new TextField();
	private TextField yearOfBirthField = new TextField();
	Button button = new Button("Add Todo Item");
	Button buttonAddUser = new Button("Add User");
	Table users;
	private CheckBox checkBox;
	private Tree tree;

	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class)
	public static class Servlet extends VaadinServlet {
	}

	@Override
	protected void init(VaadinRequest request) {

		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		users = createUserTable();
		layout.addComponent(users);
		final HorizontalLayout hLayout = new HorizontalLayout();
		hLayout.addComponent(firstNameField);
		hLayout.addComponent(lastNameField);
		hLayout.addComponent(yearOfBirthField);
		hLayout.addComponent(buttonAddUser);
		layout.addComponent(hLayout);

		todoItemLabel = new Label("Todo to add: ");
		todoItemField = new TextField();
		todoItemField.setDescription("The todo item to add");

		layout.addComponent(todoItemLabel);
		layout.addComponent(todoItemField);

		button.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				layout.addComponent(new Label(todoItemField.getValue()));
			}
		});

		buttonAddUser.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				users.addItem(
						new Object[] { firstNameField.getValue(),
								lastNameField.getValue(),
								new Integer(yearOfBirthField.getValue()) },
						new Integer(users.size()+1));
			}
		});
		layout.addComponent(button);
		
		layout.addComponent(getCheckBox());
		
		getCheckBox().setEnabled(true);
		
		
		layout.addComponent(getTree());
	}

	@Override
	public String toString() {
		// Coucou Kamil
	   return "Test same block";
	}

	private Table createUserTable() {
		if(users==null){
		/* Create the table with a caption. */
		users = new Table("This is my soratble Table");
		users.setWidth("100%");
		/*
		 * Define the names and data types of columns. The "default value"
		 * parameter is meaningless here.
		 */
		users.addContainerProperty("First Name", String.class, null);
		users.addContainerProperty("Last Name", String.class, null);
		users.addContainerProperty("Year", Integer.class, null);
		}
		return users;

	}
	
	private CheckBox getCheckBox(){
		if(checkBox==null){
			checkBox=new CheckBox("Check box");
		}
		
		return checkBox;
	}
	
	private Tree getTree()
	{
		if(tree==null){
	    // Create the Tree,a dd to layout
        tree = new Tree("Hardware Inventory");

        // Contents from a (prefilled example) hierarchical container:
        tree.setContainerDataSource(ExampleUtil.getHardwareContainer());

        // Cause valueChange immediately when the user selects
        tree.setImmediate(true);

        // Set tree to show the 'name' property as caption for items
        //tree.setItemCaptionPropertyId(ExampleUtil.hw_PROPERTY_NAME);
        tree.setItemCaptionMode(ItemCaptionMode.EXPLICIT_DEFAULTS_ID);

		}
		
		return tree;
		
	}
	
	@SuppressWarnings("unused")
	private Label getLabelFunction2(){
		return new Label("Function2");
	}
	
}

